package 力扣.二叉树相关问题;

public class 平衡二叉树 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public boolean isBalanced(TreeNode root) {
        if (root==null)
            return true;
         return height(root)>0;
    }

    private int height(TreeNode root) {
        if (root==null)
            return 0;
        int left=height(root.left);
        int right=height(root.right);
        if (left==-1 || right==-1 || Math.abs(left-right)>1)
            return -1;
        return 1+Math.max(left,right);
    }
}
